<?php
// +----------------------------------------------------------------------
// | astp [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 转盘
// +----------------------------------------------------------------------
namespace app\wewhat\controller\huodong;

use think\Controller;
use think\facade\Config;
use app\wewhat\controller\Warrant;
use app\wewhat\model\Zpuser as zpuserModel;
use think\facade\Cache;
use think\helper\Time;
use think\Db;

class Zhuanpan extends Controller
{
  // 需要授权
  public function userinfo()
  {
    $redirect_uri = urlencode(Config::get('wewhat.HTTP')."/wewhat/huodong.Zhuanpan/index");
    $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=100#wechat_redirect';
    $url = sprintf($url,Config::get('wewhat.APPID'),$redirect_uri);
    return redirect($url);
  }

  // 不需要授权
  public function base()
  {
    $redirect_uri = urlencode(Config::get('wewhat.HTTP')."/wewhat/huodong.Zhuanpan/index");
    $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=100#wechat_redirect';
    $url = sprintf($url,Config::get('wewhat.APPID'),$redirect_uri);
    return redirect($url);
  }

  public function index()
  {
    
    // dump($time); die;
    $ZpUserModel = new zpuserModel();
    $warrant = new Warrant();
    $result = $warrant->power();
    // 第二天重置抽奖次数
    list($start, $end) = Time::today();
    $update_time = Db::name('zpuser')->where('openid','=',$result['openid'])->value('update_time');
    if($update_time <= $start){
      Db::name('zpuser')->where('openid','=',$result['openid'])->update(['cnum'=>'1000']);
    }
    $rule = Db::name('zpgame')->where('type','=',1)->order('order asc')->select();
    $zpuse = $ZpUserModel->where('openid','=',$result['openid'])->find();
    if(!$zpuse){
      $ZpUserModel->save(['openid'=>$result['openid']]);
      $zpuse = $ZpUserModel->where('openid','=',$result['openid'])->find();
    }
    $prize = '';
    $chance = ''; 
    foreach($rule as $vo){
      $prize .= $vo['prize'].',';
      $chance .= $vo['chance'].',';
    }
    $prize = trim($prize,',');
    $chance = trim($chance,',');
    $this->assign([
      'rule' => $rule,  //转盘规则
      'prize' => $prize,  //奖品
      'chance' => $chance,  //概率
      'zpuse' => $zpuse //用户信息
    ]);
    return view();
  }

  public function chouj()
  {
    $user = new zpuserModel();
    if($this->request->isPost()){
      $post = Request()->param();
      $time = time();
      Db::name('zpuser')->where('openid','=',$post['openid'])->setDec('cnum');
      Db::name('zpuser')->where('openid','=',$post['openid'])->update(['update_time'=>$time]);
      if($post['char'] == 6){
        return;
      }
      $char = Db::name('zpgame')->where('order','=',$post['char'])->field('id')->find();
      $chars = Db::name('zpuser')->where('openid','=',$post['openid'])->field('gid')->find();
      $gid = $chars['gid'] . $char['id'].',';
      Db::name('zpuser')->where('openid','=',$post['openid'])->update(['gid'=>$gid]);
    }
  }

}
?>